{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Connect to Label Studio\n",
    "First we need to connect to Label Studio. We can do this by creating a new instance of the LabelStudio class and passing the base URL of the Label Studio instance and the API key as arguments. The API key can be found in the Label Studio instance under the `Account & Settings` tab."
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "6864434c3663764b"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from label_studio_sdk.client import LabelStudio\n",
    "\n",
    "# API_KEY = 'your_api_key'\n",
    "API_KEY = '27c982caa9e599c9d81b25b00663e7d4f82c9e3c'\n",
    "\n",
    "ls = LabelStudio(base_url=\"http://localhost:8080\", api_key=API_KEY)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-05T12:36:24.871545Z",
     "start_time": "2024-06-05T12:36:23.887741Z"
    }
   },
   "id": "9d71202607c33cf1",
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Import tasks from JSON\n",
    "Here we create a new project and import 3 tasks from the list of JSON objects. Each task is a dictionary with a single key `text` and a string value, which then will be used as Label Studio data column.\n",
    " \n",
    "The `import_tasks` method is used to import tasks into a project. The method takes the project ID and a list of tasks as arguments."
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ec11da5c15e156d3"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "ProjectsImportTasksResponse(task_count=3, annotation_count=0, predictions_count=None, duration=0.033486127853393555, file_upload_ids=[], could_be_tasks_list=False, found_formats=[], data_columns=[], prediction_count=0)"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "project = ls.projects.create(title=\"Example import from JSON\")\n",
    "ls.projects.import_tasks(\n",
    "    id=project.id,\n",
    "    request=[\n",
    "        {\"text\": \"Hello world\"},\n",
    "        {\"text\": \"Hello Label Studio\"},\n",
    "        {\"text\": \"What a beautiful day\"},\n",
    "    ]\n",
    ")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-05T12:36:28.283056Z",
     "start_time": "2024-06-05T12:36:28.186438Z"
    }
   },
   "id": "1227698ce462724a",
   "execution_count": 2
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
